package com.hoapital.outpbillwork.billWorkHandler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.hoapital.outpbillwork.module.controller.vo.ApplyInfo;
import com.hoapital.outpbillwork.module.controller.vo.OrderInfo;
import com.hoapital.outpbillwork.module.entity.ClinicMaster;
import com.hoapital.outpbillwork.module.entity.OutpMedrec;
import com.hoapital.outpbillwork.module.service.OutpOrderDescService;
import com.hospital.api.RemoteBillService;
import com.hospital.common.core.exception.ExceptionHelp;
import com.hospital.common.core.exception.ExceptionHelpEnum;
import com.hospital.common.core.module.controller.vo.CancelOrder;
import com.hospital.common.core.module.controller.vo.Receive;
import com.hospital.common.core.module.controller.vo.RefundBillVo;
import com.hospital.common.core.result.SuccessTip;
import com.hospital.common.core.result.Tip;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;

/**
 * @author lvyao
 * @description 混合计费业务处理类
 * @date 2021/12/1
 */
@Component
public class AllBillJob implements BillWorkHandler {
    @Resource
    private OutpOrderDescService outpOrderDescService;

    public static AllBillJob allBillJob;

    //注解@PostConstruct，这样方法就会在Bean初始化之后被Spring容器执行
    @PostConstruct
    public void init() {
        allBillJob = this;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Tip billWork(Receive receive, HttpServletRequest request) throws ExceptionHelp {
        Tip retTip = new SuccessTip();
        try {
            boolean saveBillLab = allBillJob.outpOrderDescService.saveAll(receive);
            if (saveBillLab) {
                retTip.setMessage("混合项目计费成功");
            } else {
                retTip.setCode(501);
                retTip.setMessage("混合项目项目计费失败");
            }
        } catch (ExceptionHelp r) {
            retTip.setCode(r.getCode());
            retTip.setMessage(r.getMessage());
        } catch (ParseException e) {
            retTip.setCode(503);
            retTip.setMessage("入参格式转换异常");
        }
        return retTip;
    }

    @Override
    public Tip orderWork(OrderInfo receive, HttpServletRequest request, RemoteBillService remoteBillService) {
        return null;
    }

    @Override
    public Tip outpRegistWork(OrderInfo receive, HttpServletRequest request) throws ExceptionHelp {
        return null;
    }

    @Override
    public Tip outpVisitWork(OrderInfo receive, HttpServletRequest request) throws ExceptionHelp {
        return null;
    }

    @Override
    public Tip newOrderWork(OrderInfo receive, ClinicMaster clinicMaster, OutpMedrec outpMedrec, ApplyInfo applyInfo, HttpServletRequest request, RemoteBillService remoteBillService) throws ExceptionHelp {
        return null;
    }

    @Override
    public Tip orderCancel(CancelOrder CancelOrder, HttpServletRequest request) throws ExceptionHelp {
        return null;
    }

    @Override
    public Tip sendApplyHl7File(OrderInfo orderInfo, String oneKey, String msgType) throws ExceptionHelp {
        return null;
    }

    @Override
    public Tip sendBillHl7Msg(String hosCode, String hosName, String oneKey, String type, String hl7Status) throws ExceptionHelp {
        return null;
    }

    @Override
    public Tip billInpWork(Receive requestVal, HttpServletRequest request) {
        return null;
    }

    /**
     * 退费及退结算
     *
     * @param requestVal
     * @return
     * @throws ExceptionHelp
     */
    public Tip refundBill(String requestVal, RemoteBillService remoteBillService) throws ExceptionHelp {
        RefundBillVo refundBillVo;
        try {
            refundBillVo = JSON.parseObject(requestVal, RefundBillVo.class);
        } catch (JSONException json) {
            throw new ExceptionHelp(ExceptionHelpEnum.VALIDATOR_MAP);
        }
        Tip retTip = new SuccessTip();
        try {
            retTip = allBillJob.outpOrderDescService.refundBill(refundBillVo, remoteBillService);
        } catch (Exception r) {
            retTip.setCode(502);
            retTip.setMessage(r.getMessage());
        }
        return retTip;
    }

}
